﻿// Copyright (c) Inossem. All rights reserved.
// Licensed under the LGPL-3.0 license. See LICENSE file in the project root for full license information.
using DWF.Studio.Plugin.Contract;
using DWF.Studio.Plugin.Contract.Enum;
using DWF.Studio.Plugin.Contract.Exceptions;
using DWF.Studio.Plugin.Loader.Config;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DWF.Studio.Plugin.Loader
{
    /// <summary>
    /// 插件信息
    /// </summary>
    [Serializable]
    public class PluginInfo : IPluginInfo
    {
        public PluginInfo()
        {
        }

        public PluginInfo(PluginConfig config)
         : this()
        {
            this.PluginId = config.Id;
            this.FriendlyName = config.FriendlyName;
            this.PluginFileName = config.FileName;
            this.PluginType = config.Type;
            this.Description = config.Description;
            this.Icon = config.Icon;
            this.StyleUri = config.StyleUri;
            this.StyleKey = config.StyleKey;
            this.ToolTip = config.ToolTip;
            this.GroupName = config.GroupName;
            this.Version = config.Version;
        }

        public PluginInfo(PluginConfig config, Type pluginType)
            : this(config)
        {
            this.PluginDirectory = Path.GetDirectoryName(pluginType.Assembly.Location);
            this.AssemblyFileName = pluginType.Assembly.Location;
            this.PluginTypeName = pluginType.FullName;
            this.AssemblyFullName = pluginType.Assembly.FullName;
            this.AssemblyTypes = DeterminePluginType(this.PluginFileName);
            this.PluginVersion = pluginType.Assembly.GetName().Version.ToString();

            try
            {
                this.DateLastModified = new FileInfo(this.PluginFileName).LastWriteTime;
            }
            catch (Exception)
            {
                this.DateLastModified = null;
            }
        }

        /// <summary>
        /// 判断插件类型
        /// </summary>
        /// <param name="pluginFileName"></param>
        /// <returns></returns>
        public static EPluginAssemblyType DeterminePluginType(string pluginFileName)
        {
            string ext = Path.GetExtension(pluginFileName).ToLower();
            if (ext.Equals(".exe"))
            {
                return EPluginAssemblyType.NetExecutable;
            }
            else if (ext.Equals(".dll"))
            {
                return EPluginAssemblyType.NetDynamicLinkLibrary;
            }
            else
            {
                throw new InvalidPluginFileException(pluginFileName);
            }
        }

        /// <summary>
        /// 友好名称
        /// </summary>
        public string FriendlyName
        {
            get;
            private set;
        }

        /// <summary>
        /// 插件id
        /// </summary>
        public string PluginId
        {
            get;
            private set;
        }

        /// <summary>
        /// 插件类型
        /// </summary>
        public EPluginType PluginType
        {
            get;
            private set;
        }

        /// <summary>
        /// 插件程序集类型
        /// </summary>
        public EPluginAssemblyType AssemblyTypes
        {
            get;
            private set;
        }

        /// <summary>
        /// 插件类型名称
        /// </summary>
        public string PluginTypeName
        {
            get;
            private set;
        }

        /// <summary>
        /// 程序集文件名称
        /// </summary>
        public string AssemblyFileName
        {
            get;
            private set;
        }

        /// <summary>
        /// 插件文件名称
        /// </summary>
        public string PluginFileName
        {
            get;
            private set;
        }

        /// <summary>
        /// 插件版本
        /// </summary>
        public string PluginVersion
        {
            get;
            private set;
        }

        /// <summary>
        /// 程序集全名
        /// </summary>
        public string AssemblyFullName
        {
            get;
            private set;
        }

        /// <summary>
        /// 最后一次修改时间
        /// </summary>
        public DateTime? DateLastModified
        {
            get;
            private set;
        }

        /// <summary>
        /// 描述
        /// </summary>
        public string Description
        {
            get;
            private set;
        }

        /// <summary>
        /// 提示信息
        /// </summary>
        public string ToolTip
        {
            get;
            private set;
        }

        /// <summary>
        /// 组名称
        /// </summary>
        public string GroupName
        {
            get;
            private set;
        }

        /// <summary>
        /// 版本
        /// </summary>
        public string Version
        {
            get;
            private set;
        }

        /// <summary>
        /// 插件目录
        /// </summary>
        public string PluginDirectory
        {
            get;
            private set;
        }

        public string Icon
        {
            get;
            private set;
        }

        public string StyleUri
        {
            get;
            private set;
        }

        public string StyleKey
        {
            get;
            private set;
        }
    }
}
